package com.frozenlab.android.wheretoeatai;

import java.util.Random;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Looper;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.widget.Toast;

public class AskAI extends Activity {
	/* Database helper */
	private FoodPlaceDbHelper dbHelper;
	private Spinner spinnerCategory;
	
	private ProgressDialog progressDialog;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ask_ai);
        dbHelper = new FoodPlaceDbHelper(this);
        dbHelper.open();
        
        Spinner spinner = (Spinner) findViewById(R.id.spinnerCategory);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.category_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }
    
	public void onAskAIClick(View button) {
		progressDialog = ProgressDialog.show(AskAI.this, "",
				"AI is thinking. Please wait...", true);
		new Thread() {
			public void run() {
				Looper.prepare();
				try {
					askAI();
			    	progressDialog.dismiss();
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(), e.getMessage(),
							Toast.LENGTH_LONG).show();
					progressDialog.dismiss();
				}
			}
		}.start();
	}
    
    public void askAI(){
    	spinnerCategory = (Spinner) findViewById(R.id.spinnerCategory);
    	final String selectedCategory = spinnerCategory.getSelectedItem().toString();
    	int resId = this.getResources().getIdentifier(selectedCategory, "string", this.getPackageName());
    	Cursor cursor = dbHelper.selectFoodPlaceByCategory(resId);
    	startManagingCursor(cursor);
    	final int countTotal = cursor.getCount();
    	if(countTotal > 0){
    		Random random = new Random();
    		int indexSelected = random.nextInt(countTotal);
    		cursor.moveToPosition(indexSelected);
    		final String selectedFoodPlace = cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_NAME)) ;
    		
    		AskAI.this.runOnUiThread(new Runnable() {
    		    public void run() {
    		    	TextView tvAnswer = (TextView) findViewById(R.id.textViewAnswer);
    		    	
    		    	String s1 = "AI suggest ";
    		    	String s2 = " out of " + countTotal + " food places with category " + selectedCategory;
    		    	tvAnswer.setText(s1 + selectedFoodPlace + s2, BufferType.SPANNABLE);
    		    	Spannable span = (Spannable)tvAnswer.getText();
    		    	int start = s1.length();
    		    	int end = start + selectedFoodPlace.length();
    		    	span.setSpan(new ForegroundColorSpan(0xFFFF0000), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    		    	
    		    	//Toast.makeText(AskAI.this, selectedFoodPlace, Toast.LENGTH_SHORT).show(); 
    		    }
    		});	
    		
    	}else{
    		AskAI.this.runOnUiThread(new Runnable() {
    		    public void run() {
    		    	TextView tvAnswer = (TextView) findViewById(R.id.textViewAnswer);
    		    	tvAnswer.setText("No record found for this category!");
    		    	//Toast.makeText(getApplicationContext(), "No record found for this category!", Toast.LENGTH_SHORT).show(); 
    		    }
    		});		
    	}
    }
}
